package nullUtils;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * Created by yangluhua on 2016/9/25 0025.
 */
public class NullUtils{
    /**
     * 如果是null,则获取相应类型默认的值
     */
    public static <T> T getDefaultValue(T t) throws Exception {
        if(t == null){
            if(t instanceof String){
                return (T)"";
            }else if(t instanceof List){
                return (T)Collections.EMPTY_LIST;
            }else if(t instanceof Set){
                return (T)Collections.EMPTY_SET;
            }else if(t instanceof Map){
                return (T)Collections.EMPTY_MAP;
            }else{
                throw new Exception("only support type for String and List and Set and Map");
            }
        }else{
            return t;
        }
    }
}
